<template>
  <div class="form-container">
    <el-form inline :model="form" ref="ruleForm" class="form-line" label-width="70">
      <el-form-item label="岗位名称" prop="roleName">
        <el-input size="default" v-model="form.roleName" placeholder="请输入岗位名称"></el-input>
      </el-form-item>
      <el-form-item label="所属公司" prop="companyType">
        <el-select size="default" v-model="form.companyType" placeholder="请选择所属公司">
          <el-option label="省级公司" value="1"></el-option>
          <el-option label="市级公司" value="2"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <div class="btn-container">
      <el-button type="primary" @click="handleReset(ruleForm as FormInstance)">重置</el-button>
      <el-button type="primary" @click="handleSearch">查询</el-button>
      <el-button type="primary" @click="handleAdd">添加</el-button>
    </div>
  </div>
  <div class="table-container">
    <el-table
      border
      :cell-style="{ textAlign: 'center', fontSize: '13px' }"
      :header-cell-style="{
        textAlign: 'center',
        fontWeight: '100',
        fontSize: '13px',
        color: 'white',
        backgroundColor: '#009688'
      }"
      style="width: 100%"
      :data="tableData"
    >
      <el-table-column fixed prop="roleName" label="岗位名称" />
      <el-table-column prop="postType" label="类型">
        <template #default="scope">
          {{ scope.row.postType === 1 ? "系统内置" : "手动配置" }}
        </template>
      </el-table-column>
      <el-table-column prop="companyType" label="所属公司类型">
        <template #default="scope">
          {{ scope.row.companyType === 1 ? "省级公司" : "地市公司" }}
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间" />
      <el-table-column fixed="right" label="操作">
        <template #default="scope">
          <div v-if="scope.row.postType != '1'">
            <el-button link type="primary" size="small" @click="handleDelete(scope.row.roleId)">删除</el-button>
            <el-button link type="primary" size="small" @click="handleEdit(scope.row.roleId)"> 修改 </el-button>
          </div>
          <div v-else>
            <el-button link type="primary" size="small" disabled> -------------------- </el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination-container">
      <el-pagination background size="small" layout="prev, pager, next" @change="getPost" :total="pageTotal" :page-size="10" />
    </div>
  </div>
</template>

<script setup lang="tsx" name="useProTable">
import { ElMessage, ElMessageBox, FormInstance } from "element-plus";
import { ref, onMounted } from "vue";
import { useRouter } from "vue-router";
import { Post } from "@/api/interface/province/organizationManage";
import { getPostListAPI, postDeleteAPI } from "@/api/modules/province/organizMangeAPI";
const router = useRouter();
//ProCompanyType 已经通过declare全局申明过不用再单独导入
let tableData = ref<Post.PostList[]>([]);
let pageTotal = ref<number>(0);
const ruleForm = ref<FormInstance>();
const handleEdit = (id: number) => {
  router.push({
    path: "/provinceLevel/proOrganization/proPost/add",
    query: { id }
  });
};
const handleReset = (Elform: FormInstance) => {
  Elform.resetFields();
  getPost();
};
const handleSearch = (): void => {
  console.log("查询", form.value);
  getPost();
};
const getPost = async () => {
  const { code, rows, total } = await getPostListAPI(form.value);
  if (code == "200" && rows && total) {
    tableData.value = rows;
    pageTotal.value = total;
  }
};
const handleAdd = (): void => {
  router.push("/provinceLevel/proOrganization/proPost/add");
};
const handleDelete = (id: number): void => {
  ElMessageBox.confirm(`确定删除该条记录${name}吗？`, "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning"
  })
    .then(() => {
      deletePost(id);
    })
    .catch(() => {
      ElMessage({
        type: "info",
        message: "已取消删除"
      });
    });
};
const deletePost = async (id: number) => {
  const { code, msg } = await postDeleteAPI(id);
  if (code == "200") {
    ElMessage({
      type: "success",
      message: "删除成功!"
    });
    getPost();
  } else {
    ElMessage({
      type: "error",
      message: "删除失败!" + msg
    });
  }
};
const form = ref<Post.PostSearchForm>({
  /**
   * 公司类型1-省级公司 2-市级公司
   */
  companyType: "",
  /**
   * 页数
   */
  pageNum: 1,
  /**
   * 每页数量
   */
  pageSize: 10,
  /**
   * 该用户的省份
   */
  province: JSON.parse(localStorage.getItem("userInfo") as string).province,
  /**
   * 岗位名称
   */
  roleName: ""
});
onMounted(() => {
  getPost();
});
</script>
<style lang="scss" scoped>
.form-container {
  padding: 10px;
  margin-bottom: 10px;
  background-color: white;
  .btn-container {
    display: inline-block;
    float: right;
    vertical-align: middle;
    ::v-deep .el-button > span {
      font-size: 13px;
    }
  }
  .el-form {
    display: inline-block;
    ::v-deep .el-form-item__label {
      font-size: 13px;
    }
  }
  .form-line {
    .el-form-item {
      margin: 0;
      margin-right: 20px;
    }
    .el-input {
      width: 240px;
    }
    .el-select {
      width: 240px;
    }
  }
}
.table-container {
  padding: 40px 10px;
  padding-bottom: 60px;
  background-color: white;
  .pagination-container {
    // display: inline-block;
    float: right;
    margin-top: 20px;
    background-color: white;
  }
}
</style>
